/** */
package com.hotau.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.util.Log;

/**
 * @author fengweili</br> 2011-8-22 下午06:29:59
 */
public class ValidateUtils {

	// 字符里是空字符
	public static boolean empty(String str) {
		return str.matches("^\\s*$");
	}

	// 字符里是全为数字
	public static boolean isNumberic(String str) {
		return str.matches("^\\d+$");
	}

	// 字母及数字
	public static boolean isNumberLetteric(String str) {
		return str.matches("^[a-zA-Z0-9]+$");
	}

	// 字符里是全为字母
	public static boolean isLetteric(String str) {
		return str.matches("^[a-zA-Z]+$");
	}

	// 字母和空格
	public static boolean isLettericSpace(String str) {
		return str.matches("^[A-Za-z ]+$");
	}

	// 字符里是否有空格
	public static boolean hasSpace(String str) {
		return str.matches("^\\w*\\s+\\w*$");
	}

	public static boolean hasMoreSpace(String str) {
		return str.matches("^\\w*\\s+.+\\s+\\w*$");
	}

	public static boolean hasOneSpaceBetweenOrNot(String str) {
		return str.matches("^[a-zA-Z/]+\\s?[a-zA-Z/]+$");
	}

	// 字符中间是否有空格，不在两端
	public static boolean hasSpaceBetween(String str) {
		return str.matches("^\\w+\\s+\\w+$");
	}

	// 是否是邮件
	public static boolean isEmail(String str) {
		return str
				.matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
	}

	// 是否是移动电话号码
	public static boolean isMobile(String mobile) {
		if (mobile.matches("^1\\d{10}$")) {
			return true;
		}
		return false;
	}

	// 手机号只能为13、15或18开头的11位数字
	// /^(13[0-9]{9})|(15[0-9]{9}|18[0-9]{9})$/
	public static boolean isTelePhone(String telephone) {
		if (telephone.matches("^(13[0-9]{9})|(15[0-9]{9}|18[0-9]{9})$")) {
			return true;
		}
		return false;
	}

	public static boolean isAllNumber(String number) {
		if (number.matches("^([0-9]{12})$")) {
			return true;
		}
		return false;
	}

	// 是否是合法电话号码
	public static boolean isTelephone(String telephone) {
		if (telephone.matches("^\\d{6,}$")
				|| telephone.matches("^\\d{1,}-\\d{1,}$")) {
			return true;
		}
		return false;
	}

	// 是否是会员卡号
	public static boolean isMemberCardNo(String str) {
		if (str.matches("^\\d{8}$")) {
			return true;
		}
		if (str.matches("^\\d{12}$")) {
			int sum = 0;
			for (int i = 1; i < 12; i++) {
				char c = str.charAt(i);
				sum = sum + (int) c - 48;
			}
			if (sum % 7 == ((int) str.charAt(0) - 48)) {
				return true;
			}
		}
		return false;
	}

	// 是否是英文名字
	public static boolean isEnglishName(String str) {
		return hasOneSpaceBetweenOrNot(str)
				&& str.matches("^[a-zA-Z]*\\s?[a-zA-Z]{2,}/[a-zA-Z]{2,}\\s?[a-zA-Z]*$")
				&& betweenLen(str, 2, 55);
	}

	// 是否是中文名字
	public static boolean isChineseName(String str) {
		return str.matches("^[\u4e00-\u9fa5]+$") && betweenLen(str, 2, 9);
	}

	// 是否是中英文名字
	public static boolean isChineseEnglishName(String str) {
		// return
		// str.matches("^[\u4e00-\u9fa5]{1,}[a-zA-Z]+\\s?[a-zA-Z]+$")&&betweenLen(str,
		// 3, 55);
		return str.matches("^[\u4e00-\u9fa5]{1,9}[a-zA-Z]+\\s?[a-zA-Z]+$")
				&& betweenLen(str, 3, 55);
	}

	// 姓名是否合法
	public static boolean isAvailableName(String str) {
		return isEnglishName(str) || isChineseName(str)
				|| isChineseEnglishName(str);
	}

	// 姓名是否合法
	public static boolean isNameOK(String name) {
		return name.matches("^([\\u4e00-\\u9fa5]+|([a-zA-Z]+\\s?)+)$");
	}

	// 大于多少个字符
	public static boolean maxLen(String str, int charLen) {
		return str.length() > charLen;
	}

	// 小于多少个字符
	public static boolean minLen(String str, int charLen) {
		return str.length() < charLen;
	}

	// 介于多少个字符之间,闭区间
	public static boolean betweenLen(String str, int minCharLen, int maxCharLen) {
		return str.length() >= minCharLen && str.length() <= maxCharLen;
	}

	// 是否是合法的身份证号码
	public static boolean isIdCard(String id) {

		try {
			String result = IDCard.validate(id).trim();
			Log.i("result-->", result);
			if ("".equals(result)) {
				return true;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean isMoney(String money) {
		return money.matches("([0-9]+)([.][0-9]{1,2})?");
	}

	public static void main(String[] args) {
		System.out.println(isMobile("13760879377"));
		System.out.println(isMobile("1376087937"));
		System.out.println(isMobile("137608793737"));
		System.out.println(isMobile("13760879377x"));
		System.out.println(isMobile(" 13760879377"));
		System.out.println("------------------------");
		System.out.println(isTelephone("02086930332"));
		System.out.println(isTelephone("02086930332-"));
		System.out.println(isTelephone("-02086930332"));
		System.out.println(isTelephone("020-86930332"));
		System.out.println(isTelephone("0208693033-2"));
		System.out.println("------------------------");
		System.out.println(isChineseName("中国"));
		System.out.println(isChineseName("中国 "));
		System.out.println(isChineseName("中x国"));
		System.out.println(isChineseName("中 国"));
		System.out.println("------------------------");
		System.out.println(isEnglishName("eng/lish"));
		System.out.println(isEnglishName("eng/ lish"));
		System.out.println(isEnglishName("english/"));
		System.out.println(isEnglishName("/english"));
		System.out.println(isEnglishName("english"));
		System.out.println(isEnglishName("english "));
		System.out.println(isEnglishName("eng是lish"));
		System.out.println(isEnglishName("eng lish"));
		System.out.println("------------------------");
		// System.out.println(isEnfant("2011-08-22"));
		System.out.println(isMemberCardNo("370000000003"));

	}

	public static final String NG = "NG";
	public static final String BABY = "BABY";
	public static final String ENFANT = "ENFANT";
	public static final String ADULT = "ADULT";

	// 此生日是否符合坐飞机，未满14天
	public static String whatKindPassenger(String from_yyyy_MM_dd_date,
			String to_yyyy_MM_dd_date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Calendar case1Calendar = Calendar.getInstance();
			case1Calendar.setTime(dateFormat.parse(from_yyyy_MM_dd_date));
			case1Calendar.add(Calendar.DAY_OF_YEAR, 14);
			Calendar case2Calendar = Calendar.getInstance();
			case2Calendar.setTime(dateFormat.parse(from_yyyy_MM_dd_date));
			case2Calendar.add(Calendar.YEAR, 2);
			Calendar case3Calendar = Calendar.getInstance();
			case3Calendar.setTime(dateFormat.parse(from_yyyy_MM_dd_date));
			case3Calendar.add(Calendar.YEAR, 12);

			Calendar todayCalendar = Calendar.getInstance();
			todayCalendar.setTime(dateFormat.parse(to_yyyy_MM_dd_date));

			int case1 = todayCalendar.compareTo(case1Calendar);
			int case2 = todayCalendar.compareTo(case2Calendar);
			int case3 = todayCalendar.compareTo(case3Calendar);
			if (case3 >= 0) {
				return ADULT;
			} else if (case2 > 0) {
				return ENFANT;
			} else if (case1 > 0) {
				return BABY;
			}

		} catch (ParseException e) {
			e.printStackTrace();
		}
		return NG;
	}

	// 是否是婴儿
	public static boolean isAdult(String from_yyyy_MM_dd_date,
			String to_yyyy_MM_dd_date) {
		String kind = whatKindPassenger(from_yyyy_MM_dd_date,
				to_yyyy_MM_dd_date);
		if (kind.equals(ADULT)) {
			return true;
		}
		return false;
	}

	// 是否是婴儿
	public static boolean isBaby(String from_yyyy_MM_dd_date,
			String to_yyyy_MM_dd_date) {
		String kind = whatKindPassenger(from_yyyy_MM_dd_date,
				to_yyyy_MM_dd_date);
		if (kind.equals(BABY)) {
			return true;
		}
		return false;
	}

	// 是否是儿童,不包括婴儿
	public static boolean isEnfant(String from_yyyy_MM_dd_date,
			String to_yyyy_MM_dd_date) {
		String kind = whatKindPassenger(from_yyyy_MM_dd_date,
				to_yyyy_MM_dd_date);
		if (kind.equals(ENFANT)) {
			return true;
		}
		return false;
	}

	// 是否是儿童,不包括婴儿
	public static boolean isNG(String from_yyyy_MM_dd_date,
			String to_yyyy_MM_dd_date) {
		String kind = whatKindPassenger(from_yyyy_MM_dd_date,
				to_yyyy_MM_dd_date);
		if (kind.equals(NG)) {
			return true;
		}
		return false;
	}

	// 身份证姓别判断
	private static char[] cc = { 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 };

	public static boolean checkIsID(String id) {
		if (id.length() == 18 || id.length() == 15) {
			return true;
		}
		return false;
	}

	public static String processID(String id) {
		if (id.length() == 18) {
			return id;
		} else if (id.length() == 15) {
			return make18ID(id);
		}
		throw new IllegalStateException("The count of id is not correct!");
	}

	// boy false
	public static String checkSex(char c) {
		if (c % 2 == 0)
			return "女";
		else
			return "男";
	}

	public static String checkIDSex(String id) {
		if (checkIsID(id)) {
			return checkSex(processID(id).charAt(16));
		}
		throw new IllegalStateException("The count of id is not correct!");
	}

	public static String checkIDBirthdDay(String id) {
		if (checkIsID(id)) {
			StringBuilder builder = new StringBuilder(processID(id).substring(
					6, 14));
			builder.insert(4, '-');
			builder.insert(7, '-');
			return builder.toString();
		}
		throw new IllegalStateException("The count of id is not correct!");
	}

	public static int lastNum(String id) {
		int index = 0;
		for (int i = 16; i >= 0; i--) {
			double wi = Math.pow(2, (i - 1)) % 11;
			int ai = (int) id.charAt(i);
			index += ai * wi;
		}
		index = index % 11;

		index = (int) cc[index];
		return index;
	}

	public static String make18ID(String id_15) {
		StringBuffer buffer = new StringBuffer(id_15);
		buffer.insert(6, "19");
		buffer.insert(17, lastNum(buffer.toString()));
		return buffer.toString();
	}

}
